﻿


var CalculatorAir = function (totalPriceAirElement, scheduleElement, flifgtArrivalsElement, concernElement, tourIdElement, ticketIdElement, priceAdultElement, priceChildElement, dateElement, quantityAdultElement, quantityChildElement, vatAirElement,seasonElement) {

    //ham tinh priceairbyvat
    var fncTotalPriceAir = function() {
        var priceAdults = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var quantityAdults = parseInt(quantityAdultElement);
        var quantityChild = parseInt(quantityChildElement);
        var vatAir = $(vatAirElement).val();
        var ticketId = $(ticketIdElement).val();
        var priceAir = 0;
        var priceVatAir = 0;
        if (ticketId > 0) {
            priceAir = (priceAdults * quantityAdults) + (priceChild * quantityChild);
            priceVatAir = priceAir;
            if (vatAir > 0 && vatAir <= 100) {
                priceVatAir = priceAir + priceAir * (vatAir / 100);
            }
        }


        $(totalPriceAirElement).val(priceVatAir);
    };
    //ham tinh totalpriceAir
    $(ticketIdElement).change(function () {
        var ticketId = $(this).val();
       
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "PriceTicKetAri", "TicketId": ticketId },
            dataType: "json",
            success: function (data) {
                if (data && data.isError == false) {
                    $(priceAdultElement).val(data.PriceAdult);
                    $(priceChildElement).val(data.PriceChild);
                    fncTotalPriceAir();
                }

            }
        });

    });
    //thay doi VAT
    $(vatAirElement).keyup(fncTotalPriceAir);
    
    //ham load price air
    var fncLoadTicketAir = function (ticketId) {
        var typeId = 1;
        var scheduleId = $(scheduleElement).val();
        var date = $(dateElement).val();
        var flightArrival = $(flifgtArrivalsElement).val();
        var concernId = $(concernElement).val();
        var season = seasonElement;
        if (scheduleId != -1 && flightArrival != -1 && concernId != -1 && date != NaN) {
            $.ajax({
                type: "POST",
                url: "/ajax.html",
                data: { "function": "PriceListBy", "TypeId": typeId, "ScheduleId": scheduleId, "Date": date, "FlightArrival": flightArrival, "ConcernId": concernId ,"Season":season},
                dataType: "json",
                success: function (data) {

                    $(ticketIdElement).html('<option value="-1">--- Chọn vé ---</option>');


                    if (data && data.isError == false) {
                        $.each(data.array, function (index, item) {
                            $(ticketIdElement)
                                .append($("<option></option>")
                                    .attr("value", item.ID)
                                    .text(item.Name));
                        });
                        $(ticketIdElement).val(ticketId);
                    }
                }
            });
        }

    };

    $(scheduleElement).change(fncLoadTicketAir);
    $(dateElement).change(fncLoadTicketAir);
    $(flifgtArrivalsElement).change(fncLoadTicketAir);
    $(concernElement).change(fncLoadTicketAir);
    
    $('#btnInsertAir').live("click", function () {
        var quantityAdult = parseInt(quantityAdultElement);
        var quantityChild = parseInt(quantityChildElement);
        var typeId = 1;
        var tourId = parseInt(tourIdElement);
        var ticketId = $(ticketIdElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var date = $(dateElement).val();
        var vatAir = $(vatAirElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertAir", "TypeId": typeId, "TourId": tourId, "TicketId": ticketId, "PriceAdult": priceAdult, "PriceChild": priceChild, "Date": date, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "CalculatorId": calculatorId, "ClassName": className ,'VatAir':vatAir},
            dataType: "json",
            success: function (data) {

                $(".ListAir").remove();
                $(scheduleElement).val('-1');
                $(flifgtArrivalsElement).val('-1');
                $(concernElement).val('-1');
                $(dateElement).val('');
                $(ticketIdElement + ' > option').remove();
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(vatAirElement).val('');
                $(totalPriceAirElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-ari')
                            .before($("<tr class='ListAir'></tr>").attr("id", 'ticket-air-' + item.CalculatorId).html('<td></td>' +
                                '<td class="col-schedule-name">' + item.ScheduleName + '</td>' +
                                '<td class="col-go-date">' + item.DateBegin + '</td>' +
                                '<td class="col-ticket-type">' + item.FlightArrivals + '</td>' +
                                '<td class="col-name">' + item.ConcernFullName + '</td>' +
                                '<td class="col-ticket">' + item.TicketName + '</td>' +
                                '<td class="tr col-adults">' + item.PriceAdult + '</td>' +
                                '<td class="tr col-child">' + item.PriceChild + '</td>' +
                                '<td>' + item.VatAir + '</td>' +
                                '<td class="tr col-price">' + item.TotalPriceAir + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateAir"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteAir"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalTicketAirPrices').html(data.TotalTicketAirPrices);
                    $('#TotalTicketAirPricesAdults').html(data.TotalTicketAirPricesAdults);
                    $('#TotalTicketAirPricesChild').html(data.TotalTicketAirPricesChild);
                    
                    $('#btnInsertAir').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateAir').show();
                    $('#btnDeleteAir').hide();
                }
                
            }
        });
    });
    
    $('.btnUpdateAir').live("click", function() {
        var calculatorId = $(this).attr("calculatorid");

         $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "UpdateAir", "CalculatorId": calculatorId },
            dataType: "json",
            success: function(data) {
                $('#ticket-air-' + calculatorId).remove();
                $(scheduleElement).val(data.ScheduleId);
                $(dateElement).val(data.DateBegin);
                $(flifgtArrivalsElement).val(data.FlightArrivals);
                $(concernElement).val(data.ConcernId);
                fncLoadTicketAir(data.TicketId);
                $(ticketIdElement).val(data.TicketId);
                $(priceAdultElement).val(data.PriceAdult);
                $(priceChildElement).val(data.PriceChild);
                $(vatAirElement).val(data.VatAir);
                $(totalPriceAirElement).val(data.TotalPriceAir);
                $('#btnInsertAir').attr("calculatorid", calculatorId).val("Lưu");
                $('.btnUpdateAir').hide();
                $('#btnDeleteAir').show();
            }
        });

      
    });
    
    $('.btnDeleteAir').live("click", function () {
        
        var quantityAdult = parseInt(quantityAdultElement);
        var quantityChild = parseInt(quantityChildElement);
        var typeId = 1;
        var tourId = parseInt(tourIdElement);
        var ticketId = $(ticketIdElement).val();
        var priceAdult = $(priceAdultElement).val();
        var priceChild = $(priceChildElement).val();
        var date = $(dateElement).val();
        var vatAir = $(vatAirElement).val();
        var calculatorId = $(this).attr("calculatorid");
        var className = $(this).attr("class");
        $.ajax({
            type: "POST",
            url: "/ajax.html",
            data: { "function": "InsertAir", "TypeId": typeId, "TourId": tourId, "TicketId": ticketId, "PriceAdult": priceAdult, "PriceChild": priceChild, "Date": date, "QuantityAdult": quantityAdult, "QuantityChild": quantityChild, "CalculatorId": calculatorId, "ClassName": className, 'VatAir': vatAir },
            dataType: "json",
            success: function (data) {

                $(".ListAir").remove();
                $(scheduleElement).val('-1');
                $(flifgtArrivalsElement).val('-1');
                $(concernElement).val('-1');
                $(dateElement).val('');
                $(ticketIdElement + ' > option').remove();
                $(priceAdultElement).val('');
                $(priceChildElement).val('');
                $(vatAirElement).val('');
                $(totalPriceAirElement).val('');
                if (data && data.isError == false) {
                    $.each(data.array, function (index, item) {
                        $('#new-ticket-ari')
                            .before($("<tr class='ListAir'></tr>").attr("id", 'ticket-air-' + item.CalculatorId).html('<td></td>' +
                                '<td class="col-schedule-name">' + item.ScheduleName + '</td>' +
                                '<td class="col-go-date">' + item.DateBegin + '</td>' +
                                '<td class="col-ticket-type">' + item.FlightArrivals + '</td>' +
                                '<td class="col-name">' + item.ConcernFullName + '</td>' +
                                '<td class="col-ticket">' + item.TicketName + '</td>' +
                                '<td class="tr col-adults">' + item.PriceAdult + '</td>' +
                                '<td class="tr col-child">' + item.PriceChild + '</td>' +
                                '<td>' + item.VatAir + '</td>' +
                                '<td class="tr col-price">' + item.TotalPriceAir + '</td>' +
                                '<td></td>' +
                                '<td>' +
                                '<input class="btnUpdateAir"  type="button" value="Cập nhật" calculatorid="' + item.CalculatorId + '"   />' +
                                '<input class="btnDeleteAir"  type="button" value="Xóa" calculatorid="' + item.CalculatorId + '"   />' +
                                '</td>'));

                    });
                    $('#TotalTicketAirPrices').html(data.TotalTicketAirPrices);
                    $('#TotalTicketAirPricesAdults').html(data.TotalTicketAirPricesAdults);
                    $('#TotalTicketAirPricesChild').html(data.TotalTicketAirPricesChild);

                    $('#btnInsertAir').attr("calculatorid", "-1").val("Thêm");
                    $('.btnUpdateAir').show();
                    $('#btnDeleteAir').hide();
                }

            }
        });
    });

};
